package com.ferryc.controller;

import com.ferryc.entity.Account;
import com.ferryc.model.Result;
import com.ferryc.service.IAccountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by Ferry Chen on 2019-11-06.
 */
@Api(tags = {"账户接口"})
@Slf4j
@RestController
@RequestMapping(value = "/account",produces = MediaType.APPLICATION_JSON_VALUE)
public class AccountController {


    @Autowired
    private IAccountService accountService;

    @ApiOperation(value = "新增账户", notes = "新增账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "money", value = "金额", required = false, dataType = "Number")
    })
    @PostMapping("/add")
    public Result add(@RequestParam(value = "userId") String userId,
                      @RequestParam(value = "money", required = false) Double money) {
        Account account = new Account();
        account.setUserId(userId);
        account.setMoney(money);
        Boolean result = accountService.save(account);
        if (result) {
            return Result.success("ok");
        } else {
            return Result.fail("account exists");
        }

    }

    @ApiOperation(value = "用户消费", notes = "用户消费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "money", value = "金额", required = true, dataType = "Number"),
    })
    @PostMapping("/consume")
    public Result consume(@RequestParam(value = "userId") String userId,
                          @RequestParam(value = "money") Double money) {
        Account account = new Account();
        account.setUserId(userId);
        account.setMoney(money);
        accountService.consume(userId, money);
        return Result.success("ok");
    }

    @ApiOperation(value = "用户充值", notes = "用户充值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "money", value = "金额", required = true, dataType = "Number"),
    })
    @PostMapping("/update")
    public Result update(@RequestParam(value = "userId") String userId,
                         @RequestParam(value = "money") Double money) {
        Account account = new Account();
        account.setUserId(userId);
        account.setMoney(money);
        accountService.update(account);
        return Result.success("ok");
    }

    @ApiOperation(value = "检查余额", notes = "检查余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "money", value = "金额", required = true, dataType = "Number"),
    })
    @PostMapping("/checkBalance" )
    public Result checkBalance(@RequestParam(value = "userId") String userId,
                               @RequestParam(value = "money") Double money) {
        boolean result = accountService.checkBalance(userId, money);
        if (result) {
            return Result.success(null);
        }
        return Result.fail("余额不足");
    }

}
